/*
 * 
 *方法的整理与重构
 *
 *
 */

//定义app参数名称，并加载其它模块
var authApp=angular.module('authapp', ["ngCookies",'ngFileUpload','ui.bootstrap']);

//此为定位页面所用
authApp.value('page','assessment');

/*controller部分*/


//定义父controller，主要用于controller之间传递信息
authApp.controller("fcontroller",function($scope,$http){
	//
	//****与分页相关的****
	//
	
	//从接口中获取总的分页数量
	$scope.$on('$getPageCountFromApi',function(event,data){
		$scope.$broadcast('$getPageCount',data);
	});
	
	//捕获分页点击事件并传递currentPage信息到主controller
	 $scope.$on('$pageAction',function(event,data){
		 $scope.$broadcast('$pageChanged',data);
	 });
})


//主要操作controller
authApp.controller("assessmentListController",function($scope,$http){
	//针对此页面需要的参数 should edit
	$scope.isAddM=true;
	$scope.isclickBtn=false;
	$scope.expertGName = "";
	//**********主体方法
	//设置分页初始参数（最好与$scope.currentPage和$scope.itemsPerPage一致！！！！//这边之所以$scope.pageNumber=0是因为后台接口第一页为0）
	$scope.ifFirstTimeQuery=true;
	$scope.pageNumber=0;
	$scope.pageSize=10;
	var queryMainApi=apis.queryGroups;
	$scope.queryMain=function(){
		$http({
			method:'post',
			url:queryMainApi,
			data:{
				'pageNumber':$scope.pageNumber,
				'pageSize':$scope.pageSize,
				'name':$scope.expertName,
				'group_type': '2'
			},
		}).success(function(response){
			if(response.code=="000000"){
				$scope.queryMainApiCallBack(response);
				//将返回的pagecount和current传到上面的controller
				$scope.$emit('$getPageCountFromApi',response.data);
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("网络出现故障，请确认网络是否连接");
		})
	}
	//捕捉分页点击事件并做处理
	$scope.$on("$pageChanged",function(event,data){
		if(!$scope.ifFirstTimeQuery){//解决第一次会调用两次接口的问题
			$scope.pageNumber=data.currentPage;//这边之所以-1是因为后台接口第一页为0
			$scope.queryMain();
		}
		$scope.ifFirstTimeQuery=false;//改变是否是第一次调用接口的状态
	})
	$scope.queryMain();
	
	/*下面为自已编辑部分*/
	
	//调用主接口成功之后的处理方法
	$scope.queryMainApiCallBack=function(response){
		$scope.list=response.data.data;
        $scope.totalCount = response.data.recordCount;
        $scope.pageSize = response.data.pageSize;
	}	
	/*
	 * 
	 * 其它操作方法
	 * 
	 */

	//
	$scope.addM=function(){
		$scope.isclickBtn=false;
		$scope.isAddM=false;
		$scope.expertGName = "";
	}
	$scope.cancelAddM=function(){
		$scope.isAddM=true;
		$scope.expertGName = "";
	}

	//添加专家组
	$scope.addExpertCroup= function(){
		if(window.confirm("是否确认添加现场考察专家组？")){
			$scope.isAddM=true;			
			var rJson={'name':$scope.expertGName,'type':'2'}
			$http({
				method:'post',
				url:apis.addGroup,
				data:rJson,
			}).success(function(response){
				if(response.code=="000000"){
					alert("添加现场考察专家组成功！");
					$scope.queryMain();
					$scope.expertGName="";
				}else{
					alert(response.errorMsg);
				}
			}).error(function(){
				alert("服务器打瞌睡了，请稍等！");
			})
		}
	}
	
	$scope.setExpertGroupState=function(pid,state){
		if(window.confirm("是否确认修改现场考察专家组状态？")){

			var rJson={'pid':pid,'state':state};
			$http({
				method:'post',
				url:apis.updateGroupState,
				data:rJson,
			}).success(function(response){
				if(response.code=="000000"){
					alert("修改现场考察专家组状态成功！");
					$scope.queryMain();
				}else{
					alert(response.errorMsg);
				}
			}).error(function(){
				alert("服务器打瞌睡了，请稍等！");
			})
		}
	}

	//修改专家组名称
	$scope.queryAuthGroup=function(groupId,eGname){
		$scope.isAddM=false;
		$scope.isclickBtn=true;
		$scope.Id=groupId;
		$scope.expertGName=eGname;
	}
	
	$scope.updateAuthExpertGroup=function(){
		if(window.confirm("是否确认修改现场考察专家组？")){
			$http({
				method:'post',
				url:apis.updateGroup,
				data:{'pid':$scope.Id,'name':$scope.expertGName},
			}).success(function(response){
				if(response.code=="000000"){
					$scope.isAddM=true;
					$scope.queryMain();
					$scope.expertGName="";
				}else{
					alert(response.errorMsg);
				}
			}).error(function(){
				alert("服务器打瞌睡了，请稍等！");
			})
		}
	}

	/*$scope.getSchoolList=function(pid){
		 $http({
			 method:'post',
			 url:apis.querySchoolByGroup,
			 data:{'groupId':pid},
		 }).success(function(response){
			 if(response.code=="000000"){
			 	$scope.schoolList=response.data;
			 }else{
			 	alert(response.errorMsg);
			 }
		}).error(function(){
			 alert("服务器打瞌睡了，请稍等！");
		})
	 }*/
	 $scope.deleteGroup=function(group_id) {
		if(window.confirm("是否确认删除分组？")){
			$http({
				method:'post',
				url:apis.deleteGroup,
				data:{'group_id':group_id,'group_type':2},
			}).success(function(response){
				if(response.code=="000000"){
					$scope.queryMain();
				}else{
					alert(response.errorMsg);
				}
			}).error(function(){
				alert("服务器打瞌睡了，请稍等！");
			})
		}
	}
	$scope.setSchool=function(pid){
		location.href="setGroupToSchool.html?pid="+pid;
		//$scope.getSchoolList(pid);
		//$scope.groupId = pid;
		//$scope.isSetS = false;
	}
	/*$scope.cancelSchoolAdd=function(){
		$scope.isSetS = true;
	}*/

	/*$scope.setSchoolAdd=function(){
		$http({
			method:'post',
			url:apis.setGroupToSchool,
			data:{'groupId':$scope.groupId,'schoolList':$scope.expertGName},
		}).success(function(response){
			if(response.code=="000000"){
				$scope.isAddM=true;
				$scope.queryMain();
				$scope.expertGName="";
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等！");
		})
	}*/

	$scope.turnTo=function(pid){
		location.href="experts-list.html?pid="+pid;
	}
	
})

//分页controller
authApp.controller('pageDemo',function($scope){
	//定义分页的初始参数（不改动，不会影响功能）
	$scope.currentPage=0;
	$scope.itemsPerPage=10;
	$scope.maxSize = 8;
	//需要接口返回的一些参数，如pagecount
	$scope.$on('$getPageCount',function(event,data){
      $scope.totalItems = parseInt(data.recordCount);
      $scope.bigTotalItems =parseInt(data.recordCount);      
	});
	//分页点击的触发事件
	 $scope.$watch('currentPage', function() {
		 var pagedata={currentPage:$scope.currentPage}
		 $scope.$emit('$pageAction',pagedata);
	  });
})
